package com.adopapa.nezhacloud.gateway.conf;

import java.util.Collections;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

//@Configuration
//public class CorsConfig implements WebFluxConfigurer {
//	
//	protected Logger logger = LoggerFactory.getLogger(this.getClass());
//
//	
//    /**
//     * 全局跨域配置，根据各自需求定义
//     * @param registry
//     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//    	logger.info("registry cors config ...... ");
//        registry.addMapping("/**")//
//                .allowCredentials(true)//
//                .allowedOriginPatterns("*")//
//                .allowedHeaders("*")//
//                .allowedMethods("*")//
//                .exposedHeaders(HttpHeaders.SET_COOKIE);
//    }
//
//}

@Configuration
public class CorsConfig {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Bean
	public CorsWebFilter corsFilter() {

		logger.info("registry cors config ...... ");

		// 跨域配置源
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		// 设置跨域的配置信息
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		// 允许所有请求来源进行跨域
		// corsConfiguration.addAllowedOrigin("*");
		// spring boot2.4以后的配置
		corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
		// 允许所有头进行跨域
		corsConfiguration.addAllowedHeader("*");
		// 允许所有请求方式进行跨域
		corsConfiguration.addAllowedMethod("*");
		// 允许携带cookie进行跨域
		corsConfiguration.setAllowCredentials(true);
		// 任意路径都需要跨域配置
		source.registerCorsConfiguration("/**", corsConfiguration);

		return new CorsWebFilter(source);
	}

}
